/**
 * 产品列表
 * Created by KY on 2017/4/14.
 */
define(['ldfw/core', 'art','weui',
        'text!tpl/product/list.tpl',
        'text!tpl/product/row.tpl',
        'comps/ajax', 'comps/session','jquery-weui','ldfw/helper/formatHelper'],
    function (LDFW, artTemplate, weui ,tpl,rowTpl, ajax, session) {
        return LDFW.Container.extend({
            tpl:tpl,
            events: {
                'click .buy-btn':'buy',
                'click .list-group > .list-item': 'gotoDetail',
                'click .see-more':'seeMoreProduct'
            },
            initialize: function () {
                const _this = this
                if(is_custom_skin && is_custom_skin.trim()){
                    this.model.set('is_custom_skin',is_custom_skin)
                    this.model.set('co_no',co_no)
                }
                this.once('shown', function () {
                    this.loadData()
                })
                this.loadData()
            },
            onCompAdd:function(comp){
                this.comp = comp
                this.comp.on('pullingDown',()=>{
                    this.loadData()
                })
            },
            buy:function(event){
                event.preventDefault()
                event.stopPropagation()
                let pd_no = $(event.target).attr('pd_no')
                LDFW.navigate('product/approp_confirm/'+pd_no)
                return false
            },
            loadData: function () {
                const _this = this
                let curr_cust = session.getCurrentUser(),
                    see_more = session.getItem('session_see_more')||false
                if (curr_cust.risk_accept === 0) {
                    _this.$el.find('#see-more').hide()
                    return
                }else {
                    if (see_more) {
                        $.when(ajax.post('pfL.1.210', {risk_accept: curr_cust.risk_accept}),
                            ajax.post('pfL.1.201', {risk_accept: curr_cust.risk_accept}))
                            .done(function (xhr1, xhr2) {
                                let result1 = xhr1[0].result,
                                    result2 = xhr2[0].result,
                                    result = result1.concat(result2)
                                if (!_.isEmpty(result)) {
                                    let productListHtml = artTemplate.compile(rowTpl)({list: result})
                                    _this.$el.find('#productList').html(productListHtml)
                                    setTimeout(()=> {
                                        _this.comp && _this.comp.forceUpdate()
                                    }, 100)
                                }
                            })
                    } else {
                        ajax.post('pfL.1.201', {risk_accept: curr_cust.risk_accept}).success(function (resp) {
                            if (!resp.resultCode) {
                                if (!_.isEmpty(resp.result)) {
                                    let productListHtml = artTemplate.compile(rowTpl)({list: resp.result})
                                    _this.$el.find('#productList').html(productListHtml)
                                    if (curr_cust.risk_accept >= 5 || curr_cust.invester_class == 2) {
                                        _this.$el.find('.see-more').hide()
                                    } else {
                                        _this.$el.find('.see-more').show()
                                    }
                                } else {
                                    _this.$el.find('.prodContent').html(
                                        `<div id="blank-page">
                                            <img class="blank-page" src="/assets/img/blank.png" alt="空白" display>
                                            <p>暂无匹配您风险等级的产品 </p>
                                            <p class="see-more"><a href="javascript:;">试试更高风险？</a></p>
                                        </div>`
                                    )
                                }
                                if (see_more) {
                                    _this.loadMoreProduct()
                                }
                                setTimeout(()=> {
                                    _this.comp && _this.comp.forceUpdate()
                                }, 100)
                            }
                        })
                    }
                }
            },
            gotoDetail: function (event) {
                if(event.target.tagName==='A') return false
                let curr_cust = session.getCurrentUser(),
                    pd_no = $(event.currentTarget).attr('pd_no'),
                    pd_name = $(event.currentTarget).attr('pd_name')
                if(curr_cust.qual_auth==2){
                    $.alert('对不起，您尚未通过审核，无法查看该产品的详细信息')
                }else if(curr_cust.qual_auth==1){
                    LDFW.navigate('product/detail/' + pd_no)
                }
            },

            seeMoreProduct:function(){
                const _this = this
                //查询是否已经接受过风险警示书
                ajax.post('cpliL.3.104',{mark_type:5}).success(function(resp){
                    if(resp.result.record_count<1){
                        $.confirm({
                            title: '风险警示书',
                            text: '<p>本人/机构已收到贵司出具的《风险警示函》，对于本人/机构申请查看、购买产品/服务风险等级高于本人/机构风险承受能力的情况已知悉，'+
                            '并且已充分了解该产品/服务的风险特征和可能的不利后果。</p><p>经本人/机构审慎考虑，仍坚持申请查看、购买该产品/服务，并自愿承担由此可能产生的一切不利后果和损失。'+
                            '该销售机构及工作人员在销售过程中，不存在直接或间接主动向本人/机构推介该产品/服务的行为。</p>',
                            onCancel: function(){
                            },
                            onOK:function(){
                                //留痕
                                ajax.post('cpliL.3.102',{mark_type:5}).success(function(resp){
                                    if(!resp.resultCode){
                                        let mark_no = resp.result.row_id
                                        _this.loadMoreProduct(mark_no)
                                    }
                                })
                            }
                        })
                    }else{
                        _this.loadMoreProduct()
                    }
                })
            },
            loadMoreProduct:function(mark_no){
                const _this = this
                let curr_cust = session.getCurrentUser()
                session.setItem('session_see_more',true)
                ajax.post('pfL.1.210',{risk_accept: curr_cust.risk_accept,mark_no:mark_no}).success(function(resp){
                    _this.$el.find('.see-more').hide()
                    if(!_.isEmpty(resp.result)){
                        if(_this.$el.find('#blank-page').length) {
                            _this.$el.find('#blank-page').remove()
                            _this.$el.find('.prodContent').html(`
                                <div class="list-group" id="productList">
                                </div>
                            `)
                        }
                        let productListHtml = artTemplate.compile(rowTpl)({list: resp.result})
                        _this.$el.find('#productList').append(productListHtml)
                        setTimeout(()=>{
                            _this.comp && _this.comp.forceUpdate()
                        },100)
                    }
                })
            }
        })
    })